<template>
<a-spin :spinning="saveLoading">
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">

        <a-form layout="vertical">
          <a-form-item
            label="站点标题"
          >
            <a-input v-model="formData.site_title" placeholder="站点标题" />
          </a-form-item>
          
          <a-form-item
                  label="站点Logo"
                  
          >
            <pic-uploader
                    :file-list="formData.site_logo"
            />
          </a-form-item>

<a-form-item
            label="品荐师初始化积分"
          >
            <a-input v-model="formData.seller_default_score" placeholder="品荐师初始化积分" />
          </a-form-item>

          <a-form-item
            label="砍价每日限砍次数"
          >
            <a-input v-model="formData.kanjia_day_limit" placeholder="砍价每日限砍次数" />
          </a-form-item>
          <a-form-item
            label="搜索热门关键字"
          >
            <a-input v-model="formData.h5_index_search_keywords" placeholder="搜索热门关键字" />
          </a-form-item>
          <a-form-item
            label="站点描述"
          >
            <a-textarea rows="4" v-model="formData.site_desc" placeholder="站点描述"/>
          </a-form-item>

          <a-form-item>
            <a-button @click="submitForm" :loading="saveLoading" type="primary">提交</a-button>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col :md="24" v-if="false" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="option.img"/>
        </div>
      </a-col>

    </a-row>

  </div>
  </a-spin>
</template>

<script>

  import { getSetting,setSetting } from '@/api/setting'
  import RSwitch from '@/components/RSwitch/'
  import PicUploader from '@/components/PicUploader/'
  export default {
    components: {
      PicUploader,
      RSwitch
    },
    data () {
      return {
        // cropper
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        saveLoading:false,
        form: this.$form.createForm(this),
        preview: {},
        option: {
          img: '/avatar2.jpg',
          info: true,
          size: 1,
          outputType: 'jpeg',
          canScale: false,
          autoCrop: true,
          // 只有自动截图开启 宽度高度才生效
          autoCropWidth: 180,
          autoCropHeight: 180,
          fixedBox: true,
          // 开启宽度和高度比例
          fixed: true,
          fixedNumber: [1, 1]
        },
        formData:{
          site_title:'',
          site_desc:'',
          site_logo:[], 
          seller_default_score:'', //品荐师初始化积分
          kanjia_day_limit:0, //砍价每日限砍次数
          h5_index_search_keywords:'',//搜索热门关键字
        }
      }
    },
    mounted(){
      let that = this;
      that.fetch();
    },
    methods: {

      fetch(){
        let that = this;
        getSetting({
        }) .then(res => {
          that.formData = res.data;
          that.form.setFieldsValue({...res.data});
        }).catch(err =>{
        })

      },


      submitForm(e){
        let self = this
        e.preventDefault()
        this.form.validateFields((err, values) => {
          if (!err) {
            self.saveLoading = true;
            console.log('Received values of form: ', values)
            console.log('Received valuess of formData: ', self.formData)
          setSetting({
            setting:Object.assign(self.formData,values)
          }).then(res => {
              self.$message.success('设置成功')
              self.saveLoading = false;
            }).catch(err =>{
              self.$message.error(err.data.msg);
              self.saveLoading = false;
            })
          }else{
            console.log(err);
          }
        })
      },
    }
  }
</script>


<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>